package com.joesem.sale.client.utils

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import com.jiaxing.sale.client.R
import com.joesem.sale.client.base.toast



class NavigationUtils(val context: Context) {
    private fun goToBaiduMap(address: String) {
        val intent = Intent()
        intent.data =
            Uri.parse("baidumap://map/direction?destination=$address&mode=driving&src=${context.packageName}")
        context.startActivity(intent)
        toast("正在跳转百度地图...")
    }

    private fun isInstalled(packageName: String): Boolean {
        val manager: PackageManager = context.packageManager
        val installedPackages = manager.getInstalledPackages(0)
        for (info in installedPackages) {
            if (info.packageName == packageName) return true
        }
        return false
    }

    private fun goToGaodeMap(address: String) {
        val intent = Intent(
            "android.intent.action.VIEW", Uri.parse(
                "androidamap://route?sourceApplication=${context.resources.getString(R.string.app_name)}&sname=我的位置&dname=$address&dev=0&m=0&t=0&showType=0"
            )
        )
        intent.setPackage("com.autonavi.minimap")
        context.startActivity(intent)
        toast("正在跳转高德地图...")
    }

    fun navigation(address: String) {
        if (isInstalled("com.autonavi.minimap")) {
            goToGaodeMap(address)
        } else if (isInstalled("com.baidu.BaiduMap")) {
            goToBaiduMap(address)
        } else {
            val intent = Intent(
                Intent.ACTION_VIEW,
                Uri.parse("https://uri.amap.com/marker?position=0.0,0.0&name=$address&callnative=1")
            )
            context.startActivity(intent)
            toast("正在跳转高德地图网页版...")
        }
    }
}